class_name InputHandler
extends RefCounted

## 所有需要检测的动作
var input_action_checkers: Dictionary = {}


func _init() -> void:
    # 自动加载所有引擎中配置的输入动作
    _auto_load_input_actions()


func _auto_load_input_actions() -> void:
    # 获取所有在引擎中配置的输入动作名称
    var actions: PackedStringArray = InputMap.get_actions()
    for action_name in actions:
        add_input_action(action_name)


func update_input_actions() -> void:
    input_action_checkers.clear()
    _auto_load_input_actions()


func add_input_action(action_name: String) -> void:
    var input_action_checker: InputActionChecker = InputActionChecker.new(action_name)
    input_action_checkers[action_name] = input_action_checker


func remove_input_action(action_name: String) -> void:
    input_action_checkers.erase(action_name)


func get_action_checker(action_name: String) -> InputActionChecker:
    if not input_action_checkers.has(action_name):
        Logger.error("Warning: Action '%s' not found in input_action_checkers." % action_name)
        return null
    return input_action_checkers[action_name]


func on_update(delta: float) -> void:
    for input_action_checker in input_action_checkers.values():
        input_action_checker.on_update(delta)


func is_action_pressed(action_name: String) -> bool:
    var checker = get_action_checker(action_name)
    return checker.is_pressed if checker else false


func is_action_just_pressed(action_name: String) -> bool:
    var checker = get_action_checker(action_name)
    return checker.is_just_pressed if checker else false


func is_action_pressed_any(action_names: Array[String]) -> bool:
    for action_name in action_names:
        if is_action_pressed(action_name):
            return true
    return false


func is_action_double_click_started(action_name: String) -> bool:
    var checker = get_action_checker(action_name)
    return checker.is_double_click_started if checker else false


func is_action_double_click_started_any(action_names: Array[String]) -> bool:
    for action_name in action_names:
        if is_action_double_click_started(action_name):
            return true
    return false


func is_action_long_pressed(action_name: String) -> bool:
    var checker = get_action_checker(action_name)
    return checker.is_long_pressed if checker else false


func is_action_released(action_name: String) -> bool:
    var checker = get_action_checker(action_name)
    return checker.is_released if checker else false


func get_axis(negative_action_name: String, positive_action_name: String) -> float:
    var axis := 0.0
    if is_action_pressed(negative_action_name):
        axis -= 1
    if is_action_pressed(positive_action_name):
        axis += 1
    return axis


func get_vector(
        x_negative_action_name: String,
        x_positive_action_name: String,
        y_negative_action_name: String,
        y_positive_action_name: String
    ) -> Vector2:
    return Vector2(
            get_axis(x_negative_action_name, x_positive_action_name),
            get_axis(y_negative_action_name, y_positive_action_name)
        )
